local hezou = fk.CreateSkill {
  name = "th_hezou",
  max_phase_use_time = 1,
}

hezou:addLoseEffect(function(self, player, is_death)
  player.room:setPlayerMark(player, "@th_hezou", 0)
  player.room:setPlayerMark(player, "_th_hezou", 0)
end)

hezou:addEffect("active", {
  card_num = 0,
  target_num = 0,
  prompt = function(self, player, selected_cards, selected_targets)
    local cards = player.player_cards[Player.Hand]
    local reds = #table.filter(cards, function(cid)
      return Fk:getCardById(cid).color == Card.Red
    end)
    local blacks = #table.filter(cards, function(cid)
      return Fk:getCardById(cid).color == Card.Black
    end)
    if reds ~= blacks then
      local color = reds > blacks and "red" or "black"
      return "#th_hezou-prompt:::"..color
    end
    return "#th_hezou-prompt-null"
  end,
  card_filter = function(self, to_select, selected, selected_targets)
    return false
  end,
  target_filter = function(self, player, to_select, selected)
    return false
  end,
  can_use = function(self, player)
    return not player:isKongcheng()
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local cards = player:getCardIds("h")
    player:showCards(cards)
    local reds = #table.filter(cards, function(cid)
      return Fk:getCardById(cid).color == Card.Red
    end)
    local blacks = #table.filter(cards, function(cid)
      return Fk:getCardById(cid).color == Card.Black
    end)
    if reds ~= blacks then
      room:setPlayerMark(player, "@th_hezou", reds > blacks and "red" or "black")
      room:setPlayerMark(player, "_th_hezou", reds > blacks and Card.Red or Card.Black)
    end
  end,
}, { check_skill_limit = true })

hezou:addEffect(fk.CardUseFinished, {
  is_delay_effect = true,
  max_round_use_time = 3,
  can_trigger = function(self, event, target, player, data)
    if player:getMark("_th_hezou") ~= 0 then
      if target and data.card and (data.card.type == Card.TypeBasic or data.card:isCommonTrick()) and data.card.color == player:getMark("_th_hezou") and
      #data:getAllTargets() == 1 then
        local to = data:getAllTargets()[1]
        return to and not to.dead and player:canUseTo(Fk:cloneCard(data.card.name), to, {bypass_distances = true, bypass_times = true})
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askToSkillInvoke(player, {
      skill_name = hezou.name,
      prompt = "#th_hezou-ask::"..data:getAllTargets()[1].id..":"..data.card.name
    })
  end,
  on_use = function(self, event, target, player, data)
    player.room:useVirtualCard(data.card.name, nil, player, data:getAllTargets()[1], hezou.name, false)
  end,
}, { check_effect_limit = true })
hezou:addEffect(fk.TurnStart, {
  can_refresh = function(self, event, target, player, data)
    if player:getMark("_th_hezou") ~= 0 then
      return target == player
    end
  end,
  on_refresh = function(self, event, target, player, data)
    player.room:setPlayerMark(player, "@th_hezou", 0)
    player.room:setPlayerMark(player, "_th_hezou", 0)
  end
})

Fk:loadTranslationTable{
  ["th_hezou"] = "合奏",
  [":th_hezou"] = "出牌阶段限一次，你可以展示所有手牌（至少一张），直到你的下回合开始：<br>"..
  "每轮限三次，有角色使用指定唯一目标的占展示牌多数颜色的基本牌或普通锦囊牌后，你可以视为对相同目标使用一张同名牌。",

  ["#th_hezou-prompt"] = "合奏：展示所有手牌，直到你的下回合开始：<br>\
  每轮限三次，有角色使用指定唯一目标的%arg基本牌或普通锦囊牌后，<br>\
  你可以视为对相同目标使用一张同名牌",
  ["#th_hezou-prompt-null"] = "合奏：展示所有手牌……？",

  ["#th_hezou-ask"] = "合奏：你可以视为对 %dest 使用一张 %arg",

  ["#th_hezou_trig"] = "合奏",
  ["@th_hezou"] = "合奏",
}

return hezou